"""
IP地址：47.113.180.81
账号：lemon
端口：3306
密码：lemon123
数据库：yami_shops
"""
import pymysql

from conf.setting import mysql_info

class HandleMysql:
    def __init__(self):
        self.db = pymysql.connect(host=mysql_info["host"],
                                  port=mysql_info["port"],
                                  user=mysql_info["user"],
                                  password=mysql_info["password"],
                                  db=mysql_info["db"],
                                  charset="utf8",
                                  autocommit=True,
                                  cursorclass=pymysql.cursors.DictCursor
                                  )

        self.cur = self.db.cursor()

    def close_db(self):
        self.cur.close()
        self.db.close()

    #返回元组数据,数据库断言
    def get_datas(self,sql):
        try:
            print("执行的sql语句:",sql)
            value_list = []
            self.cur.execute(sql)
            result = self.cur.fetchall()
            print("sql执行返回结果：",result)
            for dict_data in result:
                for value in dict_data.values():
                    value_list.append(value)
            return value_list
        except Exception as e:
            print("sql语句执行报错了：",e)


    #返回字典数据
    def get_data_dict(self,sql):
        try:
            print("执行的sql语句:",sql)
            self.cur.execute(sql)
            result = self.cur.fetchall()
            print("sql执行返回结果：",result)
            return result
        except Exception as e:
            print("sql语句执行报错了：",e)


mysql = HandleMysql()



if __name__ == '__main__':
    cl = HandleMysql()
    sql = "SELECT COUNT(*) FROM tz_attach_file WHERE file_path = '2022/07/570a8bd814ee48f2aa8b23a81817d432.png'"
    res = cl.get_data_dict(sql)
    res2 = cl.get_datas(sql)
    print(type(res),type(res2))

    # sql_list = ["SELECT mobile_code,user_id FROM  tz_sms_log   WHERE user_phone = '18817122161' ORDER BY id DESC LIMIT 1",
    #             "SELECT user_phone,rec_date FROM  tz_sms_log   WHERE user_phone = '18817122161' ORDER BY id DESC LIMIT 1"]
    # for sql in sql_list:
    #     res = cl.get_datas(sql) #执行sql语句，得到sql查询结果，返回的是[{}]
    #     for val in res:  #遍历list，val={“”：“”}
    #         for key,val in val.items(): #遍历字典，获取key,value
    #             pass
    #             setattr(lei,key,val)  #讲key,val设置为类属性


